package com.bkd.dreams.client.login;

import com.bkd.dreams.client.ComponentRenderer;
import com.bkd.dreams.client.volunteer.Volunteer;
import com.bkd.dreams.client.volunteer.VolunteerDTO;
import com.bkd.dreams.client.volunteer.service.VolunteerService;
import com.bkd.dreams.client.volunteer.service.VolunteerServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginRenderer {

	private VerticalPanel loginPanel = new VerticalPanel();
	private Label loginLabel = new Label("Please sign in to your Google Account to access the Achieving Dreams volunteer application.");
	private Anchor signInLink = new Anchor("Sign In");

	private VerticalPanel registrationPanel = new VerticalPanel();
	private Label volunteerNameLabel = new Label("Full name");
	private TextBox volunteerNameTextBox = new TextBox();
	private Label volunteerPhoneLabel = new Label("Phone Number");
	private TextBox volunteerPhoneTextBox = new TextBox();
	private Label volunteerEmailLabel = new Label("Email Address");
	private TextBox volunteerEmailTextBox = new TextBox();
	private CheckBox volunteerAdminCheckBox = new CheckBox();
	private Label volunteerAdminLabel = new Label("Administrator");
	private Button registerButton = new Button("Register");

	private HorizontalPanel whoAmIPanel = new HorizontalPanel();
	private Label whoAmILabel = new Label();
    private Anchor signOutLink = new Anchor("Sign Out");

	public void loadLogin(LoginInfo loginInfo) {
		// Assemble login panel.
		signInLink.setHref(loginInfo.getLoginUrl());
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get("eventList").add(loginPanel);
	}
	
	public void loadWhoAmI(LoginInfo loginInfo) {
		refreshWhoAmI(loginInfo);
		whoAmIPanel.add(whoAmILabel);
	    signOutLink.setHref(loginInfo.getLogoutUrl());
		whoAmIPanel.add(signOutLink);
		RootPanel.get("whoami").add(whoAmIPanel);
	}

	public void loadRegistration(final LoginInfo loginInfo, final ComponentRenderer table, final TabPanel tabPanel) {
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(volunteerNameLabel);
		hp.add(volunteerNameTextBox);
		registrationPanel.add(hp);
		hp = new HorizontalPanel();
		hp.add(volunteerPhoneLabel);
		hp.add(volunteerPhoneTextBox);
		registrationPanel.add(hp);
		hp = new HorizontalPanel();
		volunteerEmailTextBox.setText(loginInfo.getEmailAddress());
		hp.add(volunteerEmailLabel);
		hp.add(volunteerEmailTextBox);
		registrationPanel.add(hp);
		hp = new HorizontalPanel();
		hp.add(volunteerAdminLabel);
		hp.add(volunteerAdminCheckBox);
		registrationPanel.add(hp);
		registrationPanel.add(registerButton);
		registerButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				registerVolunteer(loginInfo, table, tabPanel);
			}
		});

        RootPanel.get("whoami").add(registrationPanel);
	}

	protected void registerVolunteer(LoginInfo loginInfo, final ComponentRenderer table, final TabPanel tabPanel) {
		String name = volunteerNameTextBox.getText();
		String phoneNumber = volunteerPhoneTextBox.getText();
		String email = volunteerEmailTextBox.getText();
		boolean admin = volunteerAdminCheckBox.getValue();
		if(name.equals("") || phoneNumber.equals("") || email.equals("")) {
			return;
		}
		VolunteerDTO volunteer = new VolunteerDTO(name, phoneNumber, email, loginInfo.getUserId(), admin);
		addVolunteer(volunteer, loginInfo, table, tabPanel);
	}

	protected void addVolunteer(VolunteerDTO volunteer, final LoginInfo loginInfo, final ComponentRenderer table, final TabPanel tabPanel) {
	    VolunteerServiceAsync volService = GWT.create(VolunteerService.class);

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				RootPanel.get("eventList").remove(registrationPanel);
				table.loadTable(tabPanel);
				refreshWhoAmI(loginInfo);
			}
		};

		// Make the call to the stock price service.
		volService.addVolunteer(volunteer, callback);
	}
	
	protected void refreshWhoAmI(LoginInfo loginInfo) {
	    VolunteerServiceAsync volService = GWT.create(VolunteerService.class);

		// Set up the callback object.
		AsyncCallback<VolunteerDTO> callback = new AsyncCallback<VolunteerDTO>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}
			public void onSuccess(VolunteerDTO result) {
				updateWhoAmI(result);
			}
		};

		// Make the call to the stock price service.
		volService.getVolunteerByUserId(loginInfo.getUserId(), callback);
	}
	
	private void updateWhoAmI(VolunteerDTO volunteer) {
		whoAmILabel.setText(volunteer.getName() + " (" + volunteer.getEmail() + ")");
	}

	protected void handleError(Throwable error) {
		Window.alert(error.getMessage());
		if (error instanceof NotLoggedInException) {
			//Window.Location.replace(loginInfo.getLogoutUrl());
		}
	}

}
